using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using AmazonCommon.Amazon.ECS;

namespace AmazonVer3.Controllers
{
    public class CartController : ApplicationController
    {
        public Cart cart
        {
            get
            { return Session["cart"] as Cart; }

            set
            {
                Session["cart"] = value;
            }
        }

        public RedirectToRouteResult Add(string ASIN, string returnUrl)
        {
            Cart currentCart;
            Item product = AmazonHelper.GetProductDetail(ASIN);

            //if there is no cart for this user
            if (cart == null || cart.CartId == null)
            {
                CartCreateRequest cartCreateRequest = new CartCreateRequest();
                CartCreateRequestItem cartItem = new CartCreateRequestItem();
                cartItem.ASIN = ASIN;
                cartItem.Quantity = 1.ToString();
                cartCreateRequest.Items = new CartCreateRequestItem[] { cartItem };
                CartCreate cartCreate = AmazonHelper.CreateCartCreate(cartCreateRequest);
                CartCreateResponse response = AmazonHelper.GetAmazonClient().CartCreate(cartCreate);
                cart = response.Cart[0];
                if (cart.Request.Errors != null)
                {                       
                    Response.Redirect(product.DetailPageURL);                    
                }
               
            }
            else
            {
                CartAddRequest cartAddRequest = new CartAddRequest();

                CartAddRequestItem cartAddItem = new CartAddRequestItem();
                cartAddRequest.CartId = cart.CartId;
                cartAddRequest.HMAC = cart.HMAC;

                cartAddItem.ASIN = ASIN;
                cartAddItem.Quantity = 1.ToString();
                cartAddRequest.Items = new CartAddRequestItem[] { cartAddItem };
                CartAdd cartAdd = AmazonHelper.CreateCartAdd(cartAddRequest);
                AWSECommerceServicePortTypeClient client = AmazonHelper.GetAmazonClient();
                CartAddResponse cartAddResponse = client.CartAdd(cartAdd);

                cart = cartAddResponse.Cart[0];
            }
            Response.Redirect(returnUrl);
            return null;
        }

        //
        // GET: /Cart/Details/5

        public ActionResult CartContents()
        {
            return View(cart);
        }

        public ActionResult UpdateCart()
        {
            CartModifyRequest cartModifyRequest = new CartModifyRequest();
            cartModifyRequest.CartId = cart.CartId;
            cartModifyRequest.HMAC = cart.HMAC;

            List<CartModifyRequestItem> cartModifyItems = new List<CartModifyRequestItem>();
            foreach (CartItem item in cart.CartItems.CartItem)
            {
                item.Quantity = Request[item.ASIN + "TextBox"];
                CartModifyRequestItem cartModifyRequestItem = new CartModifyRequestItem();
                cartModifyRequestItem.CartItemId = item.CartItemId;
                cartModifyRequestItem.Quantity = item.Quantity;
                cartModifyItems.Add(cartModifyRequestItem);
            }
            cartModifyRequest.Items = cartModifyItems.ToArray();
            CartModify cartModify = AmazonHelper.CreateCartModify(cartModifyRequest);
            AWSECommerceServicePortTypeClient client = AmazonHelper.GetAmazonClient();
            CartModifyResponse cartModifyResponse = client.CartModify(cartModify);

            cart = cartModifyResponse.Cart[0];
            if (cart.CartItems==null)
            {
                cart = null;
                Response.Redirect("~/");
            }
            return View("CartContents",cart);
        }


    }
}
